﻿using System;
using System.Threading;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Net;
using System.Collections.Generic;

namespace cb0tServer
{
    class Scribble
    {
        public static void Download(UserObject user, String url)
        {
            new Thread(new ThreadStart(delegate
            {
                String addr = url;
                String name = user.name;
                ushort vroom = user.vroom;

                try
                {
                    WebRequest request = WebRequest.Create(addr);
                    List<byte> list = new List<byte>();

                    using (WebResponse response = request.GetResponse())
                    using (Stream stream = response.GetResponseStream())
                    {
                        int size = 0;
                        byte[] buf = new byte[1024];

                        while ((size = stream.Read(buf, 0, 1024)) > 0)
                        {
                            byte[] buf2 = new byte[size];
                            Array.Copy(buf, buf2, size);
                            list.AddRange(buf2);
                        }
                    }

                    if (list.Count == 0)
                        throw new Exception();

                    using (MemoryStream ms = new MemoryStream(list.ToArray()))
                    using (Bitmap raw = new Bitmap(ms))
                    {
                        int img_x = raw.Width;
                        int img_y = raw.Height;

                        if (img_x > 384)
                        {
                            img_x = 384;
                            img_y = raw.Height - (int)Math.Floor(Math.Floor((double)raw.Height / 100) * Math.Floor(((double)(raw.Width - 384) / raw.Width) * 100));
                        }

                        if (img_y > 384)
                        {
                            img_x -= (int)Math.Floor(Math.Floor((double)img_x / 100) * Math.Floor(((double)(img_y - 384) / img_y) * 100));
                            img_y = 384;
                        }

                        using (Bitmap bmp = new Bitmap(img_x, img_y))
                        using (Graphics g = Graphics.FromImage(bmp))
                        {
                            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            g.DrawImage(raw, new RectangleF(0, 0, img_x, img_y));

                            using (MemoryStream ms2 = new MemoryStream())
                            {
                                bmp.Save(ms2, ImageFormat.Jpeg);
                                byte[] scribble = ZLib.Zlib.Compress(ms2.ToArray());
                                UserPool.BroadcastScribble(vroom, scribble);
                            }
                        }
                    }
                }
                catch
                {
                    UserPool.Broadcast(vroom, ServerOutboundPackets.AnnoucePacket("unable to download " + name + "'s scribble"));
                }
            })).Start();
        }
    }
}
